﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using CrmServerConnection;
using Microsoft.Xrm.Sdk.Client;
using Microsoft.Xrm.Sdk.Discovery;
using Microsoft.Xrm.Sdk;

namespace CrmServerConnectionTest
{
    [TestClass]
    public class CrmServerConnectionTest
    {
        [TestMethod]
        public void CrmOnlineLiveIdConnection() 
        {
            OrganizationDetailCollection orgResponse;
            CrmServerConnection.CrmConnection connection;
            connection = new CrmConnection(true, "", "", "");
            try
            {
                orgResponse = connection.GetDiscoveryService();
                connection.OrganizationName = orgResponse.First().FriendlyName;
                IServiceManagement<IOrganizationService> service = connection.ConnectToOrganization();
                OrganizationServiceProxy organizationProxy = connection.GetOrganizationProxy();
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }

        [TestMethod]
        public void CrmOffice365Connection()
        {
            OrganizationDetailCollection orgResponse;
            CrmConnection connection;
            connection = new CrmConnection(true, "", "", "");
            try
            {
                orgResponse = connection.GetDiscoveryService();
                connection.OrganizationName = orgResponse.First().FriendlyName;
                IServiceManagement<IOrganizationService> service = connection.ConnectToOrganization();
                OrganizationServiceProxy organizationProxy = connection.GetOrganizationProxy();
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }

        [TestMethod]
        public void CrmOnPremisesActiveDirectoryConnection()
        {
            OrganizationDetailCollection orgResponse;
            CrmServerConnection.CrmConnection connection;
            connection = new CrmServerConnection.CrmConnection(true, "", "", "");
            try
            {
                orgResponse = connection.GetDiscoveryService();
                connection.OrganizationName = orgResponse.First().FriendlyName;
                IServiceManagement<IOrganizationService> service = connection.ConnectToOrganization();
                OrganizationServiceProxy organizationProxy = connection.GetOrganizationProxy();
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }

        [TestMethod]
        public void CrmFederationConnection()
        {
            OrganizationDetailCollection orgResponse;
            CrmServerConnection.CrmConnection connection;
            connection = new CrmServerConnection.CrmConnection(true, "", "", "");
            try
            {
                orgResponse = connection.GetDiscoveryService();
                connection.OrganizationName = orgResponse.First().FriendlyName;
                IServiceManagement<IOrganizationService> service = connection.ConnectToOrganization();
                OrganizationServiceProxy organizationProxy = connection.GetOrganizationProxy();
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.Message);
            }
        }

    }
}
